
## load data
manipsurv <- read.csv("data/lucid experimental with manipulation check data.csv", colClasses = "character")[c(-1,-2),]

## some respondents did not take the survey after reading consent
manipsurv <- manipsurv[manipsurv$Q1 == "Yes",]

# some respondents never finished
# table(manipsurv$TradeOpenCorpsBenT1 == "" & manipsurv$TradeOpenCorpsBenT2 == "" & manipsurv$TradeOpenCorpsBenC == "")
# table(manipsurv$TradeOpenCorpsHarmT1 == "" & manipsurv$TradeOpenCorpsHarmT2 == "" & manipsurv$TradeOpenCorpsHarmC == "")
drops <- manipsurv$TradeOpenCorpsBenT1 == "" & manipsurv$TradeOpenCorpsBenT2 == "" & manipsurv$TradeOpenCorpsBenC == "" & 
  manipsurv$TradeOpenCorpsHarmT1 == "" & manipsurv$TradeOpenCorpsHarmT2 == "" & manipsurv$TradeOpenCorpsHarmC == ""
manipsurv <- manipsurv[drops == FALSE,]

## rename questions
names(manipsurv)[c(
   4:9,
   2:3,
   16:18,
   19:21)] <- 
  c("thrm_cong","thrm_corp","thrm_ent","thrm_cath","thrm_who","thrm_nfl",
   "ideology","party",
   "med_job","med_inq","med_pol",
   "employment","emp_size","emp_size_unemp")

## reassign answers for repeated questions to final answer

# treatment indicator variable
manipsurv$treatment <- "1"
manipsurv$treatment[manipsurv$TradeOpenCorpsBenT1 == "" & manipsurv$TradeOpenCorpsBenC == ""] <- "2"
manipsurv$treatment[manipsurv$TradeOpenCorpsBenT1 == "" & manipsurv$TradeOpenCorpsBenT2 == ""] <- "3"

# manipulation check questions
manipsurv$TradeOpenCorpsBenII <- ""; 
for(i in 1:nrow(manipsurv)){manipsurv$TradeOpenCorpsBenII[i] <- manipsurv[i,c(10,12,14)][manipsurv[i,c(10,12,14)] !=""]}
manipsurv$TradeOpenCorpsHarmII <- ""; 
for(i in 1:nrow(manipsurv)){manipsurv$TradeOpenCorpsHarmII[i] <- manipsurv[i,c(11,13,15)][manipsurv[i,c(11,13,15)] !=""]}
manipsurv$TradeOpenCorpsBenII <- factor(manipsurv$TradeOpenCorpsBenII, levels = 
  c("They are in industries where American companies sell their products outside the US",
    "They are in industries that face competition from foreign companies selling their products in the US",
    "They are large and very large companies","They are small and medium-sized companies",
    "All US companies benefit from trade","No US companies benefit from trade")
)
manipsurv$TradeOpenCorpsHarmII <- factor(manipsurv$TradeOpenCorpsHarmII, levels = 
  c("They are in industries where American companies sell their products outside the US",
    "They are in industries that face competition from foreign companies selling their products in the US",
    "They are large and very large companies","They are small and medium-sized companies",
    "All US companies are harmed by trade","No US companies are harmed by trade")
)

# employer size
manipsurv$emp_size_comb <- manipsurv$emp_size; manipsurv$emp_size_comb[manipsurv$emp_size_comb == ""] <- manipsurv$emp_size_unemp[manipsurv$emp_size_comb == ""]

# race
race2 <- rep("Other non-white", nrow(manipsurv))
race2[manipsurv$ethnicity == 1] <- "White"
race2[manipsurv$ethnicity == 2] <- "Black"
race2[manipsurv$hispanic %in% c(1,15) == FALSE] <- "Latino"
race2[manipsurv$hispanic %in% c(3:14)] <- "AAPI"
manipsurv$race <- factor(race2, levels = c("White","Black","Latino","AAPI","Other non-white"))

# gender
gender2 <- rep("Female", nrow(manipsurv))
gender2[manipsurv$gender == 1] <- "Male"
manipsurv$gender <- factor(gender2, levels = c("Female","Male"))

## create numeric versions of variables
# mediators convert to numeric.
manipsurv$med_job <- factor(manipsurv$med_job, levels = c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))
manipsurv$med_job <- as.numeric(manipsurv$med_job)
manipsurv$med_inq <- factor(manipsurv$med_inq, levels = c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))
manipsurv$med_inq <- as.numeric(manipsurv$med_inq)
manipsurv$med_pol <- factor(manipsurv$med_pol, levels = c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))
manipsurv$med_pol <- as.numeric(manipsurv$med_pol)

# party and ideology, turn to numeric scales
manipsurv$party <- factor(manipsurv$party, levels = c("Strong Democrat","Weak Democrat","Independent - leaning Democrat",
  "Independent - no preference","Independent - leaning Republican","Weak Republican","Strong Republican"))
manipsurv$party <- as.numeric(manipsurv$party)
manipsurv$ideology <- factor(manipsurv$ideology, levels = c("Extremely liberal","Liberal","Slightly liberal","Moderate; middle of the road",
  "Slightly conservative","Conservative","Extremely conservative"))
manipsurv$ideology <- as.numeric(manipsurv$ideology)
manipsurv$income <- as.numeric(manipsurv$hhi)
manipsurv$income[manipsurv$income == -3105] <- NA

# age
manipsurv$age <- as.numeric(manipsurv$age)

# collapse education to numeric binary
manipsurv$college <- as.numeric(manipsurv$education %in% c(4,5,6,7,8))
manipsurv$college[manipsurv$college == -3105] <- NA

# collapse employed into smaller number of categories and make numeric
manipsurv$employed <- as.numeric(manipsurv$employment %in% c("Employed full time","Employed part time"))
manipsurv$unemployed <- as.numeric(manipsurv$employment %in% c("Unemployed looking for work","Unemployed not looking for work"))
manipsurv$retstudis <- as.numeric(manipsurv$employment %in% c("Retired","Student","Disasbled"))

manipsurv$emp_size_comb <- factor(manipsurv$emp_size_comb, c("1-5","6-19","20-49","50-199","200-999","1,000-9,999","More than 10,000"))
manipsurv$emp_size_comb <- as.numeric(manipsurv$emp_size_comb)

# convert empsize into a above/below median dummy
manipsurv$emp_size_big <- as.numeric(as.numeric(manipsurv$emp_size_comb) > median(as.numeric(manipsurv$emp_size_comb), na.rm = TRUE))

# convert used thermometer into above/below median dummies.
manipsurv$thrm_corp_pos <- as.numeric(as.numeric(manipsurv$thrm_corp) > median(as.numeric(manipsurv$thrm_corp), na.rm = TRUE))

## create cleaned version of the data for analysis
manipsurv <- manipsurv[,c("TradeOpenCorpsBenII","TradeOpenCorpsHarmII",
  "treatment",
  "age","gender","race",
  "college","income","employed","unemployed",
  "party","ideology",
  "thrm_corp","thrm_corp_pos","emp_size_comb","emp_size_big",
  "med_job","med_inq","med_pol"
)]

num_vars <- c("age",  
  "college","income","employed","unemployed",
  "party","ideology",
  "thrm_corp","thrm_corp_pos","emp_size_comb","emp_size_big",
  "med_job","med_inq","med_pol")
manipsurv[,num_vars] <- apply(manipsurv[,num_vars], 2, as.numeric)

####################################
## Manipulation check large/small ##
####################################

## relative to control
manipsurvT1relC <- manipsurv[manipsurv$treatment %in% c("1","3"),]
mod1 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies") ~ I(treatment=="1"), data =manipsurvT1relC); 
mod2 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies") ~ I(treatment=="1") 
  + age + gender + race, data =manipsurvT1relC);
mod3 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies") ~ I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed, data =manipsurvT1relC);
mod4 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies") ~ I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data =manipsurvT1relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC1_corpsben_LS_T1relC.tex")

# 
mod1 <- lm(I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ I(treatment=="1"), data =manipsurvT1relC); 
mod2 <- lm(I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ I(treatment=="1") 
  + age + gender + race, data =manipsurvT1relC);
mod3 <- lm(I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed, data =manipsurvT1relC);
mod4 <- lm(I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data =manipsurvT1relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC1_corpsharm_LS_T1relC.tex")

# 
mod1 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies" & TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ 
  I(treatment=="1"), data =manipsurvT1relC); 
mod2 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies" & TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ 
  I(treatment=="1") 
  + age + gender + race, data =manipsurvT1relC);
mod3 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies" & TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ 
  I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed, data =manipsurvT1relC);
mod4 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies" & TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ 
  I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data =manipsurvT1relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC1_CompoundBeliefs_LS_T1relC.tex")

## Relative to T2
manipsurvT1relT2 <- manipsurv[manipsurv$treatment %in% c("1","2"),]
mod1 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies") ~ I(treatment=="1"), 
  data = manipsurvT1relT2); 
mod2 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies") ~ I(treatment=="1") 
  + age + gender + race, data = manipsurvT1relT2);
mod3 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies") ~ I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed, data = manipsurvT1relT2);
mod4 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies") ~ I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = manipsurvT1relT2);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC1_corpsben_LS_T1relT2.tex")

# 
mod1 <- lm(I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ I(treatment=="1"), 
  data = manipsurvT1relT2);
mod2 <- lm(I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ I(treatment=="1") 
  + age + gender + race, data = manipsurvT1relT2);
mod3 <- lm(I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed, data = manipsurvT1relT2);
mod4 <- lm(I(TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = manipsurvT1relT2);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC1_corpsharm_LS_T1relT2.tex")

# 
mod1 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies" & TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ 
  I(treatment=="1"), 
  data = manipsurvT1relT2);
mod2 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies" & TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ 
  I(treatment=="1") 
  + age + gender + race, data = manipsurvT1relT2);
mod3 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies" & TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ 
  I(treatment=="1")
  + age + gender + race + college + income + employed + unemployed, data = manipsurvT1relT2);
mod4 <- lm(I(TradeOpenCorpsBenII == "They are large and very large companies" & TradeOpenCorpsHarmII == "They are small and medium-sized companies") ~ 
  I(treatment=="1") 
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = manipsurvT1relT2);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC1_CompoundBeliefs_LS_T1relT2.tex")

#####################################
## Manipulation check exp/imp-comp ##
#####################################

manipsurvT2relC <- manipsurv[manipsurv$treatment %in% c("2","3"),]
mod1 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US") ~ I(treatment=="2"), 
  data = manipsurvT2relC);
mod2 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US") ~ I(treatment=="2")  
  + age + gender + race, data =manipsurvT2relC);
mod3 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US") ~ I(treatment=="2") 
  + age + gender + race + college + income + employed + unemployed, data =manipsurvT2relC);
mod4 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US") ~ I(treatment=="2") 
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data =manipsurvT2relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T2: Exporters ben./Import-competors harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC2_corpsben_EI_T2relC.tex")

# 
mod1 <- lm(I(TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ I(treatment=="2") , data =manipsurvT2relC);
mod2 <- lm(I(TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ I(treatment=="2")  
  + age + gender + race, data =manipsurvT2relC);
mod3 <- lm(I(TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ I(treatment=="2") 
  + age + gender + race + college + income + employed + unemployed, data =manipsurvT2relC);
mod4 <- lm(I(TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ I(treatment=="2") 
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data =manipsurvT2relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T2: Exporters ben./Import-competors harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC2_corpsharm_EI_T2relC.tex")

# 
mod1 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US" & TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ 
  I(treatment=="2"), data =manipsurvT2relC);
mod2 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US" & TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ 
  I(treatment=="2")  
  + age + gender + race, data =manipsurvT2relC);
mod3 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US" & TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ 
  I(treatment=="2") 
  + age + gender + race + college + income + employed + unemployed, data =manipsurvT2relC);
mod4 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US" & TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ 
  I(treatment=="2") 
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data =manipsurvT2relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T2: Exporters ben./Import-competors harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC2_CompoundBeliefs_EI_T2relC.tex")

## Relative to T2
manipsurvT1relT2 <- manipsurv[manipsurv$treatment %in% c("1","2"),]
mod1 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US") ~ I(treatment=="2"), 
  data = manipsurvT1relT2);
mod2 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US") ~ I(treatment=="2") 
  + age + gender + race, data = manipsurvT1relT2);
mod3 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US") ~ I(treatment=="2")
  + age + gender + race + college + income + employed + unemployed, data = manipsurvT1relT2);
mod4 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US") ~ I(treatment=="2")
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = manipsurvT1relT2);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T2: Exporters ben./Import-competors harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC2_corpsben_EI_T2relT1.tex")

# 
mod1 <- lm(I(TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ I(treatment=="2"), 
  data = manipsurvT1relT2);
mod2 <- lm(I(TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ I(treatment=="2") 
  + age + gender + race, data = manipsurvT1relT2);
mod3 <- lm(I(TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ I(treatment=="2")
  + age + gender + race + college + income + employed + unemployed, data = manipsurvT1relT2);
mod4 <- lm(I(TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ I(treatment=="2")
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = manipsurvT1relT2);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T2: Exporters ben./Import-competors harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC2_corpsharm_EI_T2relT1.tex")

# 
mod1 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US" & TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ 
  I(treatment=="2"), 
  data = manipsurvT1relT2);
mod2 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US" & TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ 
  I(treatment=="2") 
  + age + gender + race, data = manipsurvT1relT2);
mod3 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US" & TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ 
  I(treatment=="2")
  + age + gender + race + college + income + employed + unemployed, data = manipsurvT1relT2);
mod4 <- lm(I(TradeOpenCorpsBenII == "They are in industries where American companies sell their products outside the US" & TradeOpenCorpsHarmII == "They are in industries that face competition from foreign companies selling their products in the US") ~ 
  I(treatment=="2") 
  + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = manipsurvT1relT2);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- c("T2: Exporters ben./Import-competors harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC2_CompoundBeliefs_EI_T2relT1.tex")


